package com.bright.ghj.user.cas;

import com.bright.ghj.user.properties.CasProperties;
import com.bright.ghj.user.util.SecurityUtil;
import org.springframework.security.authentication.AuthenticationDetailsSource;
import org.springframework.security.cas.web.authentication.ServiceAuthenticationDetails;

import javax.servlet.http.HttpServletRequest;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * @Author txf
 * @Date 2025/11/8 16:40
 * @Description
 */
public class CasServiceAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest, ServiceAuthenticationDetails> {

    private CasProperties casProperties;

    public CasServiceAuthenticationDetailsSource(CasProperties casProperties) {
        this.casProperties = casProperties;
    }


    @Override
    public ServiceAuthenticationDetails buildDetails(HttpServletRequest request) {

        String domain;
        try {
            domain = SecurityUtil.getDomainOfRequestUrl(request);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        String appServerUrl = SecurityUtil.getMatchedDomainUrl(casProperties.getAppServerUrl(), domain);

        //https://hl3z.jieyang.gov.cn/jxmap_api/login
        return new CasServiceAuthenticationDetails(appServerUrl + casProperties.getAppLoginUrl(), casProperties);
    }
}
